import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.qiyueoffic.dao.TableBrandsDAO;
import com.qiyueoffic.dao.TableBrandsDAOImpl;
import com.qiyueoffic.dao.TableTypesDAO;
import com.qiyueoffic.dao.TableTypesDAOImpl;
import com.qiyueoffic.model.TableBrands;
import com.qiyueoffic.model.TableBrandsExample;
import com.qiyueoffic.model.TableTypes;
import com.qiyueoffic.model.TableTypesExample;


public class TestDB {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("system power on ... ");
		String resource = "sqlmap-config.xml";


        SqlMapClient sqlMap = null;
        try
        {
            Reader reader = Resources.getResourceAsReader(resource);
            sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        
        //test
        TableTypesDAO dao=new TableTypesDAOImpl(sqlMap);
        
        TableBrandsDAO brandsDao=new TableBrandsDAOImpl(sqlMap);
        try {
        	TableTypesExample typeExample=new TableTypesExample();
        	typeExample.setOrderByClause("");
        	 
        	int count=dao.countByExample(typeExample);
        	System.out.println("count:"+count);
			
        	 TableTypes type=new TableTypes();
             //type.setTypeid(count);
             type.setTypename("刘红邦");
        	 //dao.insert(type);
        	 
        	 typeExample.setOrderByClause("typeid");
        	 List typeList=dao.selectByExample(typeExample);
        	 for(int i=0;i<typeList.size();i++)
        	 {
        		 TableTypes typeIndex=(TableTypes)typeList.get(i);
        		 System.out.println("index:"+typeIndex.getTypeid()+" name:"+typeIndex.getTypename());
        	 }
        	
        	 TableBrands brands=new TableBrands();
        	 //brands.setBrandid(2);
        	 brands.setBrandname("hp");
        	 File image = new File("E:/lxb/code/TestIbatis/bin/index.jpg");
        	 
        	 brands.setBrandicon(getBytesFromFile(image));
        	 //brandsDao.insert(brands);
        	 
        	 //取图片
        	 TableBrandsExample brandsExample=new TableBrandsExample();
        	 brandsExample.setOrderByClause("brandid=1");
        	 List brandList=brandsDao.selectByExampleWithBLOBs(brandsExample);
        	 TableBrands brandsResult=new TableBrands();
        	 brandsResult=(TableBrands)brandList.get(0);
        	 
        	 System.out.println("图片大小"+brandsResult.getBrandicon().length);
        	 File imageFile=getFileFromBytes(brandsResult.getBrandicon(),"E:/lxb/code/TestIbatis/bin/index2.jpg");
        	 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        System.out.println("exit ... ");

	}
	
	private static byte[] getImage()
	{
		File image = new File("E:/lxb/code/TestIbatis/bin/index.jpg");

		byte[] buffer = new byte[60240];
		try
		{
		BufferedInputStream br = new BufferedInputStream(new FileInputStream(image));
		br.read(buffer);
		int bufferLength=buffer.length;
		System.out.println("bufferLength:"+bufferLength);
		}
		catch (FileNotFoundException ex)
		{
		   System.out.println("error FileNotFoundException");
		}
		catch (IOException ex)
		{
		//...
		}
		return buffer;

	}

	public static byte[] getBytesFromFile(File f){  
	    if (f == null){  
	        return null;  
	    }  
	    try {  
	        FileInputStream stream = new FileInputStream(f);  
	        ByteArrayOutputStream out = new ByteArrayOutputStream(10000);  
	        byte[] b = new byte[10000];  
	        int n;  
	        while ((n = stream.read(b)) != -1)  
	            out.write(b, 0, n);  
	        stream.close();  
	        out.close();  
	        return out.toByteArray();  
	    } catch (IOException e){  
	    }  
	    return null;  
	}  
	
	public static byte[] image2Bytes(String imagePath) {
		  ImageIcon ima = new ImageIcon(imagePath);
		  BufferedImage bu = new BufferedImage(ima.getImage().getWidth(null), ima
		    .getImage().getHeight(null), BufferedImage.TYPE_INT_RGB);
		  ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
		  try {
		   //锟斤拷锟斤拷锟�pg图锟斤拷写锟斤拷锟斤拷锟斤拷锟斤拷锟饺�锟斤拷锟斤拷锟斤拷锟阶�拷锟酵计�拷谋锟斤拷锟斤拷式
		   boolean resultWrite = ImageIO.write(bu, "jpg", imageStream);
		  } catch (IOException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
		  }
		  byte[] tagInfo = imageStream.toByteArray();
		  int bufferLength=tagInfo.length;
		  System.out.println("bufferLength:"+bufferLength);
		  return tagInfo;
	}
	
	public static File getFileFromBytes(byte[] b, String outputFile){  
	    BufferedOutputStream stream = null;  
	    File file = null;  
	    try {  
	        file = new File(outputFile);  
	        FileOutputStream fstream = new FileOutputStream(file);  
	        stream = new BufferedOutputStream(fstream);  
	        stream.write(b);  
	    } catch (Exception e){  
	        e.printStackTrace();  
	    } finally{  
	        if (stream != null){  
	            try {  
	                stream.close();  
	            } catch (IOException e1) {  
	                e1.printStackTrace();  
	            }  
	        }  
	    }  
	    return file;  
	}  
}
